﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Lab_1_MultiDBAccess
{
    public partial class ConnForm : Form
    {
        public string connStr;
        public ConnForm()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            connStr = String.Format(@"Data Source={0};Initial Catalog={1}; User Id={2}; Password={3};", tbServer.Text, tbDB.Text, tbUser.Text, tbPassword.Text);
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            string connStr = String.Format(@"Data Source={0};Initial Catalog={1}; User Id={2}; Password={3};", tbServer.Text, tbDB.Text, tbUser.Text, tbPassword.Text);
            SqlConnection conn = new SqlConnection(connStr);
            try
            {
                conn.Open();
                MessageBox.Show(String.Format("Connected to Databse: {0}\nState: {1}", conn.Database, conn.State), "Success!");
            }
             catch (Exception ex)
            {
                MessageBox.Show(String.Format("Connection Error\n Reason:{0}",ex.Message), "Error");
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
